﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Windows.Interop;

namespace OmniKits.Windows.Interop
{
    public partial class CoverForm : Form
    {
        private IWindowPosition _posModel;
        private System.Windows.Window _owner;

        private CoverForm()
        {
            InitializeComponent();

            TransparencyKey = BackColor;
        }

        public CoverForm(System.Windows.Window owner)
            : this()
        {
            if (owner == null) throw new ArgumentNullException("owner");

            _owner = owner;

            owner.Closed += delegate { Close(); };
            owner.IsVisibleChanged += delegate { Visible = owner.IsVisible; };
            owner.StateChanged += delegate { WindowState = (FormWindowState)owner.WindowState; };

            _posModel = owner.DataContext as IWindowPosition;
            if (_posModel != null)
            {
                Left = _posModel.Left.ToWindowPosInt();
                Top = _posModel.Top.ToWindowPosInt();
                Width = _posModel.Width.ToWindowPosInt();
                Height = _posModel.Height.ToWindowPosInt();
                TopMost = _posModel.Topmost;

                _posModel.PropertyChanged += PosModel_PropertyChanged;
            }
        }

        void PosModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Left":
                    Left = _posModel.Left.ToWindowPosInt();
                    break;
                case "Top":
                    Top = _posModel.Top.ToWindowPosInt();
                    break;
                case "Width":
                    Width = _posModel.Width.ToWindowPosInt();
                    break;
                case "Height":
                    Height = _posModel.Height.ToWindowPosInt();
                    break;
                case "Topmost":
                    TopMost = _posModel.Topmost;
                    break;
            }
        }

        protected override void WndProc(ref Message m)
        {
            switch (m.Msg)
            {
                case User32.WM_DWMCOMPOSITIONCHANGED:
                    Handle.SetWindowDisplayAffinity(WindowDisplayAffinity.Monitor);
                    return;
            }

            base.WndProc(ref m);
        }

        protected override void CreateHandle()
        {
            base.CreateHandle();
            var wih = new WindowInteropHelper(_owner);
            Handle.SetWindowLongPtr(WindowLongIndex.OwnerHandle, wih.Handle);
            Handle.SetWindowDisplayAffinity(WindowDisplayAffinity.Monitor);
        }

        protected override CreateParams CreateParams
        {
            get
            {
                var value = base.CreateParams;
                value.ExStyle |= User32.WS_EX_TRANSPARENT;
                return value;
            }
        }
    }
}
